package com.wx.activitys.drp.controller;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSONObject;
import com.github.sd4324530.fastweixin.api.JsAPI;
import com.github.sd4324530.fastweixin.api.OauthAPI;
import com.github.sd4324530.fastweixin.api.UserAPI;
import com.github.sd4324530.fastweixin.api.enums.OauthScope;
import com.github.sd4324530.fastweixin.api.response.GetSignatureResponse;
import com.github.sd4324530.fastweixin.api.response.GetUserInfoResponse;
import com.github.sd4324530.fastweixin.api.response.OauthGetTokenResponse;
import com.wx.activitys.base.controller.BaseController;
import com.wx.activitys.drp.model.BUserInfo;
import com.wx.activitys.drp.service.UserInfoService;

@Controller
@RequestMapping(value = "/enCenter")
public class EnCenterController extends BaseController {
	@Autowired
	private OauthAPI authAPI;
	@Autowired
	private JsAPI jsAPI;
	@Autowired
	private UserAPI userAPI;
	@Autowired
	private UserInfoService userInfoService;

	public static final String ROOT_NAME = "drp";
	public static final String BASE_PATH = "http://jh.xuanzhen-tech.com";
	public static final String BASE_URL = BASE_PATH;
	public static final String REDIRECT_URL = BASE_URL + "/enCenter/dealAuthUser.html";

	@RequestMapping(value = "auth2.html", method = RequestMethod.GET)
	public void auth2(HttpSession session, HttpServletResponse response, HttpServletRequest request)
			throws IOException {
		String redirectURL = authAPI.getOauthPageUrl(REDIRECT_URL, OauthScope.SNSAPI_USERINFO, "STATE");
		String onOpenId = request.getParameter("onOpenId");
		session.setAttribute("onOpenId", onOpenId);
		response.sendRedirect(redirectURL);
	}

	@RequestMapping(value = "dealAuthUser.html", method = RequestMethod.GET)
	public String dealAuthUser(HttpSession session, HttpServletRequest request) {
		String code = request.getParameter("code");
		OauthGetTokenResponse authToken = authAPI.getToken(code);
		GetUserInfoResponse userInfo = authAPI.getUserInfo(authToken.getAccessToken(), authToken.getOpenid());
		GetUserInfoResponse userInfo1 = userAPI.getUserInfo(authToken.getOpenid());
		userInfo.setOpenid(authToken.getOpenid());
		userInfo.setSubscribe(userInfo1.getSubscribe());
		logger.info("openid为" + authToken.getOpenid());
		logger.info(userInfo);
		session.setAttribute("userInfo", userInfo);
		return ROOT_NAME + "/centerRedirect";
	}

	@RequestMapping(value = "index.html", method = RequestMethod.GET)
	public String index(Model model, HttpServletRequest request, HttpServletResponse response,HttpSession session) {
		GetUserInfoResponse userInfo = ((GetUserInfoResponse) request.getSession().getAttribute("userInfo"));
		if (userInfo != null) {
				String currentPage = BASE_URL + "/index.html";
				GetSignatureResponse signObj = jsAPI.getSignature(currentPage);
				model.addAttribute("signObj", signObj);
				model.addAttribute("basePath", BASE_PATH);
				BUserInfo buserInfo = userInfoService.findByOpenid(userInfo.getOpenid());
				if(buserInfo!=null){
					if(buserInfo.getState()==0){
						return ROOT_NAME + "/error/error";
					}else{
						if (StringUtils.isNotBlank(buserInfo.getAuthInfo())) {
							JSONObject jsStr = JSONObject.parseObject(buserInfo.getAuthInfo());
							model.addAttribute("authInfo", jsStr);
						}
						model.addAttribute("buserInfo", buserInfo);
						session.setAttribute("user", buserInfo);
						return ROOT_NAME + "/user/user";		
					}					
				}else{
					return ROOT_NAME + "/register";
				}
		} else {
			return "redirect:/enCenter/auth2.html";
		}
	}
}
